package com.wangc.report.controller;

import com.wangc.report.entity.SysUserEntity;
import com.wangc.report.service.SysUserService;
import com.wangc.report.utils.PageUtils;
import com.wangc.report.utils.Query;
import com.wangc.report.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户Controller
 *
 * @author wangc
 * @email 1620415297@qq.com
 * @date 2020-04-01 23:31:37
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<SysUserEntity> sysUserList = sysUserService.queryList(query);
        int total = sysUserService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(sysUserList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId) {
        SysUserEntity sysUser = sysUserService.queryObject(userId);

        return R.ok().put("sysUser", sysUser);
    }

    @RequestMapping("/info")
    public R info() {
        return R.ok().put("user", "admin");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SysUserEntity sysUser) {
        sysUserService.save(sysUser);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SysUserEntity sysUser) {
        sysUserService.update(sysUser);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] userIds) {
        sysUserService.deleteBatch(userIds);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<SysUserEntity> list = sysUserService.queryList(params);

        return R.ok().put("list", list);
    }
}
